﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:tendercompanyinfo
	/// </summary>
	public partial class tendercompanyinfo
	{
		public tendercompanyinfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("tcid", "tendercompanyinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int tcid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tendercompanyinfo");
			strSql.Append(" where tcid=@tcid");
			SqlParameter[] parameters = {
					new SqlParameter("@tcid", SqlDbType.Int,4)
			};
			parameters[0].Value = tcid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.tendercompanyinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tendercompanyinfo(");
			strSql.Append("tcbh,tcname,tcadd,tclink,tcphone,tcfax,tcemail,tcjlid)");
			strSql.Append(" values (");
			strSql.Append("@tcbh,@tcname,@tcadd,@tclink,@tcphone,@tcfax,@tcemail,@tcjlid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@tcbh", SqlDbType.NVarChar,10),
					new SqlParameter("@tcname", SqlDbType.NVarChar,50),
					new SqlParameter("@tcadd", SqlDbType.NVarChar,150),
					new SqlParameter("@tclink", SqlDbType.NVarChar,10),
					new SqlParameter("@tcphone", SqlDbType.NVarChar,20),
					new SqlParameter("@tcfax", SqlDbType.NVarChar,20),
					new SqlParameter("@tcemail", SqlDbType.NVarChar,50),
					new SqlParameter("@tcjlid", SqlDbType.Int,4)};
			parameters[0].Value = model.tcbh;
			parameters[1].Value = model.tcname;
			parameters[2].Value = model.tcadd;
			parameters[3].Value = model.tclink;
			parameters[4].Value = model.tcphone;
			parameters[5].Value = model.tcfax;
			parameters[6].Value = model.tcemail;
			parameters[7].Value = model.tcjlid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.tendercompanyinfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tendercompanyinfo set ");
			strSql.Append("tcbh=@tcbh,");
			strSql.Append("tcname=@tcname,");
			strSql.Append("tcadd=@tcadd,");
			strSql.Append("tclink=@tclink,");
			strSql.Append("tcphone=@tcphone,");
			strSql.Append("tcfax=@tcfax,");
			strSql.Append("tcemail=@tcemail,");
			strSql.Append("tcjlid=@tcjlid");
			strSql.Append(" where tcid=@tcid");
			SqlParameter[] parameters = {
					new SqlParameter("@tcbh", SqlDbType.NVarChar,10),
					new SqlParameter("@tcname", SqlDbType.NVarChar,50),
					new SqlParameter("@tcadd", SqlDbType.NVarChar,150),
					new SqlParameter("@tclink", SqlDbType.NVarChar,10),
					new SqlParameter("@tcphone", SqlDbType.NVarChar,20),
					new SqlParameter("@tcfax", SqlDbType.NVarChar,20),
					new SqlParameter("@tcemail", SqlDbType.NVarChar,50),
					new SqlParameter("@tcjlid", SqlDbType.Int,4),
					new SqlParameter("@tcid", SqlDbType.Int,4)};
			parameters[0].Value = model.tcbh;
			parameters[1].Value = model.tcname;
			parameters[2].Value = model.tcadd;
			parameters[3].Value = model.tclink;
			parameters[4].Value = model.tcphone;
			parameters[5].Value = model.tcfax;
			parameters[6].Value = model.tcemail;
			parameters[7].Value = model.tcjlid;
			parameters[8].Value = model.tcid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int tcid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tendercompanyinfo ");
			strSql.Append(" where tcid=@tcid");
			SqlParameter[] parameters = {
					new SqlParameter("@tcid", SqlDbType.Int,4)
			};
			parameters[0].Value = tcid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string tcidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tendercompanyinfo ");
			strSql.Append(" where tcid in ("+tcidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.tendercompanyinfo GetModel(int tcid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 tcid,tcbh,tcname,tcadd,tclink,tcphone,tcfax,tcemail,tcjlid from tendercompanyinfo ");
			strSql.Append(" where tcid=@tcid");
			SqlParameter[] parameters = {
					new SqlParameter("@tcid", SqlDbType.Int,4)
			};
			parameters[0].Value = tcid;

			lcs.Model.tendercompanyinfo model=new lcs.Model.tendercompanyinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.tendercompanyinfo DataRowToModel(DataRow row)
		{
			lcs.Model.tendercompanyinfo model=new lcs.Model.tendercompanyinfo();
			if (row != null)
			{
				if(row["tcid"]!=null && row["tcid"].ToString()!="")
				{
					model.tcid=int.Parse(row["tcid"].ToString());
				}
				if(row["tcbh"]!=null)
				{
					model.tcbh=row["tcbh"].ToString();
				}
				if(row["tcname"]!=null)
				{
					model.tcname=row["tcname"].ToString();
				}
				if(row["tcadd"]!=null)
				{
					model.tcadd=row["tcadd"].ToString();
				}
				if(row["tclink"]!=null)
				{
					model.tclink=row["tclink"].ToString();
				}
				if(row["tcphone"]!=null)
				{
					model.tcphone=row["tcphone"].ToString();
				}
				if(row["tcfax"]!=null)
				{
					model.tcfax=row["tcfax"].ToString();
				}
				if(row["tcemail"]!=null)
				{
					model.tcemail=row["tcemail"].ToString();
				}
				if(row["tcjlid"]!=null && row["tcjlid"].ToString()!="")
				{
					model.tcjlid=int.Parse(row["tcjlid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select tcid,tcbh,tcname,tcadd,tclink,tcphone,tcfax,tcemail,tcjlid,[user_name],[user_mobelPhone],[user_email]");
			strSql.Append(" FROM Vtendercompanyinfo where 1=1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" tcid,tcbh,tcname,tcadd,tclink,tcphone,tcfax,tcemail,tcjlid ");
			strSql.Append(" FROM tendercompanyinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tendercompanyinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.tcid desc");
			}
			strSql.Append(")AS Row, T.*  from tendercompanyinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tendercompanyinfo";
			parameters[1].Value = "tcid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

